<?php

/**
 * 会员中心
 */
namespace User\Controller;
use Common\Controller\MemberbaseController;
class ProfileController extends MemberbaseController {
	
	protected $db;
	function _initialize(){
		parent::_initialize();
		$this->db=D("Common/Users");
	}
	
    //编辑用户资料
	public function edit() {
		$userid=$this->u[id];
		
		if(IS_POST){
		
			$_POST['id']=$userid;
			if ($this->db->create()) {
				if ($this->db->save()!==false) {
				
					$this->_do_login_data($userid);
					$this->success("保存成功！",U("Center/index"));
				} else {
					$this->error("保存失败！");
				}
			} else {
				$this->error($this->users_model->getError());
			}
		}
		else{
		$user=$this->db->where(array("id"=>$userid))->find();
		$this->assign("user",$user);
    	$this->display();
    	
	}
    }

    
    public function password() {
    	$userid=$this->u[id];
    	
    	if (IS_POST) {
    		if(empty($_POST['old_password'])){
    			$this->error("原始密码不能为空！");
    		}
    		if(empty($_POST['password'])){
    			$this->error("新密码不能为空！");
    		}
    		
    		if(strlen($_POST['password']) < 5 || strlen($_POST['password']) > 20){
    			$this->error("密码长度至少5位，最多20位！");
    		}
    	
    		$admin=$this->db->where("id=$userid")->find();
    		//$this->error($admin['pass'].lap_password($old_password));
    		$old_password=$_POST['old_password'];
    		$password=$_POST['password'];
    		if(lap_password($old_password,$admin['pwd_hash'])==$admin['pass']){
    			if($_POST['password']==$_POST['repassword']){
    				if($admin['pass']==lap_password($password,$admin['pwd_hash'])){
    					$this->error("新密码不能和原始密码相同！");
    				}else{
    					$data['pass']=lap_password($password,$admin['pwd_hash']);
    					$data['id']=$userid;
    					$r=$this->db->save($data);
    					if ($r!==false) {
    						$this->success("修改成功！");
    					} else {
    						$this->error("修改失败！");
    					}
    				}
    			}else{
    				$this->error("密码输入不一致！");
    			}
    			 
    		}else{
    			$this->error("原始密码不正确！");
    		}
    	}
    	else{
    	$user=$this->db->where(array("id"=>$userid))->find();
    	$this->assign($user);
    	$this->display();
    	}
    }
    
    
    
    function bang(){
    	$oauth_user_model=M("OauthUser");
    	$uid=$this->u[id];
    	$oauths=$oauth_user_model->where(array("uid"=>$uid))->select();
    	$new_oauths=array();
    	foreach ($oauths as $oa){
    		$new_oauths[strtolower($oa['from'])]=$oa;
    	}
    	$this->assign("oauths",$new_oauths);
    	$this->display();
    }
    

    

    
    
}
    
